package com.cicigodd.brick.plug.system.menu.factory;

import com.cicigodd.brick.plug.common.tree.node.ElementTreeNode;
import com.cicigodd.brick.plug.system.menu.response.Meta;

import cn.hutool.core.util.ObjectUtil;
import com.cicigodd.brick.plug.common.tree.factory.DefaultTreeBuildFactory;
import com.cicigodd.brick.plug.common.tree.factory.node.SystemTreeConstants;
import com.cicigodd.brick.plug.system.menu.entity.SysMenu;
import com.cicigodd.brick.plug.system.menu.response.RouterResponse;

import java.util.ArrayList;
import java.util.List;

/**
 * 针对于element vue版本的前端菜单的组装
 *
 * @author cicigodd
 * @date 2020/12/30 20:11
 */
public class EleMenusFactory {

    /**
     * 组装获取所有菜单列表详情
     *
     * @param menuList 菜单集合
     * @author cicigodd
     * @date 2021/1/7 18:17
     */
    public static List<RouterResponse> createTotalMenus(List<SysMenu> menuList) {
        //把菜单实体转化成树
        List<SysMenu> treeStructMenu = new DefaultTreeBuildFactory<SysMenu>(SystemTreeConstants.DEFAULT_PARENT_ID.toString()).doTreeBuild(menuList);
        return doModelTransfer(treeStructMenu);
    }

    /**
     * 把菜单转化成路由
     *
     * @author cicigodd
     * @date 2021/3/23 21:40
     */
    private static List<RouterResponse> doModelTransfer(List<SysMenu> sysMenuList) {
        if (ObjectUtil.isEmpty(sysMenuList)) {
            return null;
        } else {
            ArrayList<RouterResponse> resultMenus = new ArrayList<>();

            for (SysMenu sysMenu : sysMenuList) {

                if(SystemTreeConstants.DEFAULT_PARENT_ID.toString().equals(sysMenu.getParentId().toString())&&sysMenu.getMenuType().equals("C")){
                    RouterResponse routerResponse = new RouterResponse();
                    RouterResponse response = new RouterResponse();
                    Meta meta = new Meta();
                    meta.setIcon(sysMenu.getIcon());
                    meta.setNoCache(false);
                    meta.setTitle(sysMenu.getMenuName());
                    response.setPath(sysMenu.getRouter());
                    response.setComponent(sysMenu.getComponent());
                    response.setAlwaysShow(false);
                    response.setName(sysMenu.getRouter());
                    response.setHidden(false);
                    response.setMeta(meta);

                    List<RouterResponse> list = new ArrayList<>();
                    list.add(response);
                    routerResponse.setComponent("Layout");
                    routerResponse.setHidden(false);
                    routerResponse.setPath("/");
                    routerResponse.setChildren(list);
                    resultMenus.add(routerResponse);
                }else{
                    RouterResponse routerResponse = new RouterResponse();
                    Meta meta = new Meta();
                    meta.setIcon(sysMenu.getIcon());
                    meta.setLink("");
                    meta.setNoCache(false);
                    meta.setTitle(sysMenu.getMenuName());

                    //判断是目录
                    if (sysMenu.getMenuType().equals("M")) {
                        routerResponse.setPath("/" + sysMenu.getRouter());
                        routerResponse.setComponent("Layout");
                        routerResponse.setRedirect("noRedirect");
                    } else {
                        routerResponse.setPath(sysMenu.getRouter());
                        routerResponse.setComponent(sysMenu.getComponent());
                    }
                    routerResponse.setAlwaysShow(false);
                    routerResponse.setName(sysMenu.getRouter());
                    routerResponse.setHidden(false);
                    routerResponse.setMeta(meta);
                    if (ObjectUtil.isNotEmpty(sysMenu.getChildren())) {
                        routerResponse.setChildren(doModelTransfer(sysMenu.getChildren()));
                    }
                    resultMenus.add(routerResponse);
                }
            }

            return resultMenus;
        }
    }

    /**
     * menu实体转化为菜单树节点
     *
     * @author cicigodd
     * @date 2020/11/23 21:54
     */
    public static ElementTreeNode parseMenuBaseTreeNode(SysMenu sysMenu) {
        ElementTreeNode menuTreeNode = new ElementTreeNode();
        menuTreeNode.setId(sysMenu.getId());
        menuTreeNode.setParentId(sysMenu.getParentId());
        menuTreeNode.setLabel(sysMenu.getMenuName());
        return menuTreeNode;
    }

    /**
     * 添加根节点
     *
     * @author cicigodd
     * @date 2021/4/16 15:52
     */
    public static ElementTreeNode createRootNode() {
        ElementTreeNode elementTreeNode = new ElementTreeNode();
        elementTreeNode.setId(-1);
        elementTreeNode.setParentId(-2);
        elementTreeNode.setLabel("根节点");
        return elementTreeNode;
    }

    /**
     * 填充叶子节点的标识
     *
     * @author cicigodd
     * @date 2021/8/8 15:22
     */
    public static void fillLeafFlag(List<SysMenu> sysMenuList) {
        for (SysMenu sysMenu : sysMenuList) {
            sysMenu.setLeafFlag(true);

            // 判断这个节点下面有没有节点
            for (SysMenu tempMenu : sysMenuList) {
                if (tempMenu.getMenuPids().contains("[" + sysMenu.getId() + "]")) {
                    sysMenu.setLeafFlag(false);
                }
            }
        }
    }


}
